﻿CREATE PROCEDURE [acms].[Post_GetAllByTopicIdsAndParentPath]
	@TopicIds varchar(1000), 
	@ParentPath varchar(255),
	@SiteVersion DateTime
AS
	declare @tmp DateTime
	
	set @tmp = acms.GetPublishDate(@SiteVersion)

	if(@tmp is null and @SiteVersion is not null)
		return

	if not Right(@ParentPath, 1) = '/'
		set @ParentPath = @ParentPath + '/'
	
	declare @tmpTopics table (Id int)
	
	set @TopicIds = RTrim(LTrim(@TopicIds))
	if(Left(@TopicIds, 1) = '|')
		set @TopicIds = Right(@TopicIds, Len(@TopicIds) - 1)
	if(Right(@TopicIds, 1) = '|')
		set @TopicIds = Left(@TopicIds, Len(@TopicIds) - 1)
	
	while CHARINDEX ('|', @TopicIds) > 0 begin
		declare @tmpId int
		set @tmpId = Left(@TopicIds, CHARINDEX ('|' ,@TopicIds) - 1) 
		insert into @tmpTopics (Id) values( cast(@tmpId as int))
		set @TopicIds = Right(@TopicIds, Len(@TopicIds) - CHARINDEX ('|', @TopicIds)) 
	end 
	

	SELECT distinct a.*, b.ParentPath, b.PublishDate as SiteVersion
	FROM [Post_View] a
		inner join [PostPositions] b on a.Id = b.PostId and isnull(b.PublishDate, '9999/12/31') = isnull(@tmp, '9999/12/31')
		inner join [PostTopics] c on a.Id = c.PostId and c.Deleted = 0
	Where c.TopicId in (select Id from @tmpTopics)
		and b.ParentPath = @ParentPath
	ORDER By Sequence

RETURN 0;